package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.policeasset.component.AssetsCode;
import com.zygh.policeasset.component.PurchaseAssetsCode;
import com.zygh.policeasset.mapper.ApprovalProcessMapper;
import com.zygh.policeasset.mapper.AssetsMapper;
import com.zygh.policeasset.mapper.AssetsNoMapper;
import com.zygh.policeasset.mapper.DepartmentMapper;
import com.zygh.policeasset.mapper.PurchaseMapper;
import com.zygh.policeasset.mapper.UnitMapper;
import com.zygh.policeasset.pojo.ProcessRecord;
import com.zygh.policeasset.pojo.Project;
import com.zygh.policeasset.pojo.Purchase;
import com.zygh.policeasset.utils.EasyExcelUtil;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/service/PurchaseService.class */
public class PurchaseService {
    @Autowired
    PurchaseMapper purchaseMapper;
    @Autowired
    AssetsNoMapper assetsNoMapper;
    @Autowired
    UnitMapper unitMapper;
    @Autowired
    ProcessRecordSerivce processRecordSerivce;
    @Autowired
    ApprovalProcessMapper approvalProcessMapper;
    @Autowired
    PurchaseAssetsCode purchaseAssetsCode;
    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    EasyExcelUtil easyExcelUtil;
    @Autowired
    AssetsCode assetsCode;
    @Autowired
    AssetsMapper assetsMapper;

    public Integer add(Purchase purchase, String add) {
        int count = 0;
        try {
            purchase.setStatusNum("0");
            if ("1".equals(add)) {
                count = this.purchaseMapper.add(purchase);
            } else {
                purchase.setPactAppNum(0);
                purchase.setMoneyAppNum(0);
                count = this.purchaseMapper.updateById(purchase);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf(count);
    }

    public Integer add1(Purchase purchase, String add) {
        int count = 0;
        try {
            purchase.setStatusNum("0");
            if ("1".equals(add)) {
                count = this.purchaseMapper.add(purchase);
            } else {
                purchase.setPactAppNum(0);
                purchase.setMoneyAppNum(0);
                count = this.purchaseMapper.updateById(purchase);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf(count);
    }

    public Integer submit(String projectNo) {
        int count = 0;
        try {
            count = this.purchaseMapper.submit(projectNo, "4");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf(count);
    }

    public Integer app(Purchase purchase, String result) {
        ProcessRecord processRecord = new ProcessRecord();
        processRecord.setProjectId(purchase.getProjectId());
        processRecord.setProcessName("cg");
        processRecord.setProcessTime(DateUtil.now());
        processRecord.setApproverId(purchase.getPactAppNum().toString());
        processRecord.setOpinion(purchase.getPactAppContent());
        int count = 0;
        try {
            if ("1".equals(result)) {
                List<Purchase> purchaseList = this.purchaseMapper.findOneAll(purchase.getProjectNo(), null, null);
                for (int i = 0; i < purchaseList.size(); i++) {
                    Purchase purchaselod = purchaseList.get(i);
                    purchase.setId(purchaselod.getId());
                    String purchaseNum = this.unitMapper.findOneByName("cg", purchaselod.getConditionSectionName());
                    StringBuffer purchaseNo = new StringBuffer();
                    purchaseNo.append("ZD");
                    purchaseNo.append(purchaseNum);
                    purchaseNo.append("0101");
                    purchaseNo.append(purchaselod.getGetDatetime().replace("-", ""));
                    for (int j = 0; j < 5 - String.valueOf(i + 1).length(); j++) {
                        purchaseNo.append("0");
                    }
                    purchaseNo.append(i + 1);
                    purchase.setPurchaseNo(String.valueOf(purchaseNo));
                    count = this.purchaseMapper.app(purchase);
                    processRecord.setStatus(5);
                    this.processRecordSerivce.updateStatus(processRecord).intValue();
                }
                this.approvalProcessMapper.insertToSelectc(-1, "cg", purchase.getPactAppContent(), purchase.getPactAppNum().toString(), DateUtil.now(), purchase.getProjectNo());
            } else {
                count = this.purchaseMapper.apperror(purchase);
                processRecord.setStatus(6);
                this.processRecordSerivce.updateStatus(processRecord).intValue();
                this.approvalProcessMapper.insertToSelectc(1, "cg", purchase.getPactAppContent(), purchase.getPactAppNum().toString(), DateUtil.now(), purchase.getProjectNo());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf(count);
    }

    public Integer updateById(Purchase purchase) {
        return Integer.valueOf(this.purchaseMapper.updateById(purchase));
    }

    public Integer deleteById(int id) {
        return Integer.valueOf(this.purchaseMapper.deleteById(Integer.valueOf(id)));
    }

    public Map findById(int id) {
        return this.purchaseMapper.findById(Integer.valueOf(id));
    }

    public PageInfo findOneAll(String projectNo, int pageNum, int pageSize, String purchaseName, Integer assetsClassificationId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.purchaseMapper.findOneAll(projectNo, purchaseName, assetsClassificationId));
    }

    public PageInfo findByStatus(Integer status, Integer dataLabelsId, String name,
                                 Integer pageNum, Integer pageSize, Integer userId,
                                 Integer unitId, Double combined) {
        PageHelper.startPage(pageNum.intValue(), pageSize.intValue());
        List<Project> list1 = new ArrayList<>();
        List<Project> list = this.purchaseMapper.findByStatus(status, dataLabelsId, name, userId, unitId);
        if (!StringUtils.isEmpty(combined)) {
            for (Project project : list) {
                Double d = this.assetsMapper.selectCombined(null, null, project.getProjectNo(), null, null, null);
                if (!StringUtils.isEmpty(d) && new BigDecimal(d.doubleValue()).compareTo(new BigDecimal(combined.doubleValue())) == 0) {
                    list1.add(project);
                }
            }
        } else {
            list1 = list;
        }
        return new PageInfo<>(list1);
    }

    public Project findbyId(Integer id) {
        return this.purchaseMapper.findbyId(id);
    }

    public String findByStatusExcel(Integer status, Integer dataLabelsId, String name, HttpServletResponse response, Integer userId, Integer unitId) {
        return this.easyExcelUtil.findByCompanyExcel(response, this.purchaseMapper.findByStatus(status, dataLabelsId, name, userId, unitId));
    }

    public Integer findAssetsAppCount(Integer userId) {
        return this.purchaseMapper.findAssetsAppCount(userId);
    }

    public Integer updateStartProvisionDate(String startProvisionDate, String buildNum) {
        return this.purchaseMapper.updateStartProvisionDate(startProvisionDate, buildNum);
    }

    public Integer updassetsClassificationId(String ids, Integer assetsClassificationId, Integer assetsClassificationPid) {
        Integer count = 0;
        for (String str : ids.split(",")) {
            this.purchaseMapper.updateassetsClassification(Integer.valueOf(Integer.parseInt(str)), assetsClassificationId, assetsClassificationPid);
            count = Integer.valueOf(count.intValue() + 1);
        }
        return count;
    }

    public Integer findByBuildNum(String buildNum) {
        return this.purchaseMapper.findByBuildNum(buildNum);
    }
}